<template>
  <div class="login-container">
    <div class="wrapper">
      <div class="flip-card-container">
        <!-- 开关 -->
        <input v-model="isSignUp" type="checkbox" class="toggle" id="flip-toggle" :disabled="loading">
        <div class="card-switch">
          <label class="switch" for="flip-toggle">
            <span class="slider"></span>
            <span class="card-side"></span>
          </label>
        </div>

        <!-- 卡片容器 -->
        <div class="flip-card__inner">
          <!-- 登录面 -->
          <div class="flip-card__front">
            <LoginForm @update:loading="loading = $event" />
          </div>

          <!-- 注册面 -->
          <div class="flip-card__back">
            <RegisterForm @update:loading="loading = $event" @registration-success="isSignUp = false" />
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import LoginForm from '../components/LoginForm.vue';
import RegisterForm from '../components/RegisterForm.vue';

const isSignUp = ref(false);
const loading = ref(false); // 父组件维护一个loading状态，防止在请求中翻转卡片
</script>

<style scoped>
/* 这里只保留共享的、与布局相关的样式 */
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 20px 0;
}

.wrapper {
  --input-focus: #667eea;
  --font-color: #323232;
  --font-color-sub: #666;
  --bg-color: #fff;
  --main-color: #764ba2;
}

.flip-card-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  perspective: 1000px;
}

.card-switch {
  padding-top: 40px;
  margin-bottom: 20px;
}

.switch {
  position: relative;
  display: inline-block;
  width: 50px;
  height: 20px;
}

.card-side::before {
  position: absolute;
  content: '登录';
  left: -70px;
  top: 0;
  width: 100px;
  text-decoration: underline;
  color: #fff;
  font-weight: 600;
  font-size: 16px;
}

.card-side::after {
  position: absolute;
  content: '注册';
  left: 70px;
  top: 0;
  width: 100px;
  text-decoration: none;
  color: #fff;
  font-weight: 600;
  font-size: 16px;
}

.toggle {
  opacity: 0;
  width: 0;
  height: 0;
}

.slider {
  box-sizing: border-box;
  border-radius: 5px;
  border: 2px solid var(--main-color);
  box-shadow: 4px 4px rgba(0, 0, 0, 0.2);
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: var(--bg-color);
  transition: 0.3s;
}

.slider:before {
  box-sizing: border-box;
  position: absolute;
  content: "";
  height: 20px;
  width: 20px;
  border: 2px solid var(--main-color);
  border-radius: 5px;
  left: -2px;
  bottom: 2px;
  background-color: var(--bg-color);
  box-shadow: 0 3px 0 var(--main-color);
  transition: 0.3s;
}

.toggle:checked + .card-switch .slider {
  background-color: var(--input-focus);
}

.toggle:checked + .card-switch .slider:before {
  transform: translateX(30px);
}

.toggle:checked + .card-switch .card-side:before {
  text-decoration: none;
}

.toggle:checked + .card-switch .card-side:after {
  text-decoration: underline;
}

.flip-card__inner {
  width: 440px;
  min-height: 480px;
  height: auto;
  margin-bottom: 40px;
  position: relative;
  background-color: transparent;
  text-align: center;
  transition: transform 0.8s;
  transform-style: preserve-3d;
}

.toggle:checked ~ .flip-card__inner {
  transform: rotateY(180deg);
}

.flip-card__front,
.flip-card__back {
  padding: 40px;
  position: absolute;
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 100%;
  height: 100%;
  -webkit-backface-visibility: hidden;
  backface-visibility: hidden;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  gap: 20px;
  border-radius: 16px;
  border: 2px solid var(--main-color);
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  overflow-y: auto;
}

.flip-card__front {
  justify-content: center;
}

.flip-card__back {
  justify-content: flex-start;
  transform: rotateY(180deg);
}

/* 以下为子组件会用到的共享样式 */
:deep(.flip-card__form) {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 15px;
  width: 100%;
}

:deep(.title) {
  font-size: 28px;
  font-weight: 900;
  text-align: center;
  color: var(--main-color);
  margin-bottom: 5px;
}

:deep(.subtitle) {
  font-size: 14px;
  color: var(--font-color-sub);
  text-align: center;
  margin-bottom: 10px;
}

:deep(.flip-card__input) {
  width: 100%;
  height: 45px;
  border-radius: 8px;
  border: 2px solid #ddd;
  background-color: var(--bg-color);
  font-size: 15px;
  font-weight: 500;
  color: var(--font-color);
  padding: 5px 15px;
  outline: none;
  transition: all 0.3s ease;
}

:deep(input[type="date"]:not(:valid)) {
  color: var(--font-color-sub);
}

:deep(.flip-card__input::placeholder) {
  color: var(--font-color-sub);
  opacity: 0.7;
}

:deep(.flip-card__input:focus) {
  border: 2px solid var(--input-focus);
  box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}

:deep(.password-wrapper) {
  position: relative;
  width: 100%;
}

:deep(.password-wrapper .flip-card__input) {
  padding-right: 45px;
}

:deep(.eye-button) {
  position: absolute;
  right: 10px;
  top: 50%;
  transform: translateY(-50%);
  background: none;
  border: none;
  cursor: pointer;
  font-size: 20px;
  padding: 5px;
  line-height: 1;
  opacity: 0.6;
  transition: opacity 0.3s;
}

:deep(.eye-button:hover) {
  opacity: 1;
}

:deep(.flip-card__btn) {
  margin-top: 15px;
  width: 100%;
  max-width: 220px;
  height: 45px;
  border-radius: 8px;
  border: none;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  box-shadow: 0 4px 15px rgba(102, 126, 234, 0.4);
}

:deep(.flip-card__btn:hover:not(:disabled)) {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(102, 126, 234, 0.6);
}

:deep(.flip-card__btn:active:not(:disabled)) {
  transform: translateY(0);
}

:deep(.flip-card__btn:disabled) {
  opacity: 0.6;
  cursor: not-allowed;
}
</style>